package application;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import tools.EraserTool;
import tools.FreehandTool;
import tools.ObjectSelectorTool;
import tools.TextTool;
import tools.TwoEndShapeTool;
import tools.shapes.FilledOvalShape;
import tools.shapes.FilledRectangleShape;
import tools.shapes.LineShape;
import tools.shapes.OvalShape;
import tools.shapes.RectangleShape;

/**
 * Provides a ToolBar-type view displaying the MiniDraw Tools supplied.  
 * Toolbars generally consist of picture-buttons sometimes accompanied with 
 * text.
 *
 * Displays the associated buttons tools within the supplied ToolList.  The
 * ToolList itself contains the various tool controllers that will be associated
 * with this view.
 * 
 * For more details, see the documentation of ToolController and ToolList.
 */
@SuppressWarnings("serial")
public class ToolBarView extends JPanel
{
	// Components
	protected JButton button;
	protected CanvasView canvas;
	
	/**
	 * Constructor that adds the tool if is not null to the ToolBar
	 * 
	 * @param actions Associated ObjectDraw Tool Controller
	 */
	public ToolBarView(String [] name, CanvasView c)
	{ 
		setLayout(new GridLayout(10, 1, 5, 15));
		if(c != null)
		{
			canvas = c;
		}
		
		int count = 0;
		while (count < name.length)
		{
			if(name[count] == "Rectangle")
			{
			
				button = new JButton("Rectangle");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TwoEndShapeTool(canvas, new RectangleShape()), "Rectangle");
							}	
						}
				);
			}
			else if (name[count] == "Object")
			{
				button = new JButton("Object selector");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new ObjectSelectorTool(canvas), "Object");
							}	
						}
				);
			}
			else if (name[count] == "Line")
			{
				button = new JButton("Line");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TwoEndShapeTool(canvas, new LineShape()), "Line");
							}	
						}
				);
			}
			else if (name[count] == "Freehand")
			{
				button = new JButton("Freehand");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new FreehandTool(canvas), "Freehand");
							}	
						}
				);
			}
			else if (name[count] == "Eraser")
			{
				button = new JButton("Erase");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new EraserTool(canvas), "Eraser");
							}	
						}
				);
			}
			else if (name[count] == "Oval")
			{
				button = new JButton("Oval");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TwoEndShapeTool(canvas, new OvalShape()), "Oval");
							}	
						}
				);
			}
			else if (name[count] == "FillOval")
			{
				button = new JButton("Solid oval");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TwoEndShapeTool(canvas, new FilledOvalShape()), "FillOval");
							}	
						}
				);
			}
			else if (name[count] == "FillRect")
			{
				button = new JButton("Solid rectangle");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TwoEndShapeTool(canvas, new FilledRectangleShape()), "FillRect");
							}	
						}
				);
			}
			else if (name[count] == "Text")
			{
				button = new JButton("Text");
				button.addActionListener(
						new ActionListener()
						{
							@Override
							public void actionPerformed(ActionEvent e){
								canvas.setcurrentTool(new TextTool(canvas), "Text");
							}	
						}
				);
			}
			
			add(button);
			count++;
		}
	}
}
